﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Game.Klassen;

namespace Game.Classes
{
    class Tools
    {

        #region Allgemeine Tools
        //Crop Image
        public static Image cropImage(Rectangle cropRect, Image source)
        {
            Bitmap src = source as Bitmap;
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
            Graphics g = Graphics.FromImage(target);

            g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), 
                            cropRect,                        
                            GraphicsUnit.Pixel);

            return (Image)target;
        }

        //Rotate Image in Direkton
        public static Image rotateImage(Image img, String rotation)
        {
            RotateFlipType type = RotateFlipType.RotateNoneFlipNone;
            if (rotation == "N")
                type = RotateFlipType.RotateNoneFlipNone;
            if (rotation == "O")
                type = RotateFlipType.Rotate90FlipNone;
            if (rotation == "S")
                type = RotateFlipType.Rotate180FlipNone;
            if (rotation == "W")
                type = RotateFlipType.Rotate270FlipNone;

            img.RotateFlip(type);
            return img;
        }

        //Hilfsmethode die ein Array in einen String Schreibt und zurückgibt
        public static string ArrayToString(string[] array, string delimiter)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string value in array)
            {
                builder.Append(value);
                builder.Append(delimiter);
            }
            return builder.ToString();
        }

        //Spielerbild basierend auf nummer
        public static Image getPlayerStandImage(int playerNumber)
        {
            Image img;

            //Spielerbild 1-8
            if (playerNumber == 0)
                img = Game.Properties.Resources.player1_stand;
            else if (playerNumber == 1)
                img = Game.Properties.Resources.player2_stand;
            else if (playerNumber == 2)
                img = Game.Properties.Resources.player3_stand;
            else if (playerNumber == 3)
                img = Game.Properties.Resources.player4_stand;
            else if (playerNumber == 4)
                img = Game.Properties.Resources.player5_stand;
            else if (playerNumber == 5)
                img = Game.Properties.Resources.player6_stand;
            else if (playerNumber == 6)
                img = Game.Properties.Resources.player7_stand;
            else
                img = Game.Properties.Resources.player8_stand;

            return img;
        }

        //Spielerbild basierend auf nummer
        public static Image getPlayerMoveImage(int playerNumber)
        {
            Image img;

            //Spielerbild 1-8
            if (playerNumber == 0)
                img = Game.Properties.Resources.player1_move;
            else if (playerNumber == 1)
                img = Game.Properties.Resources.player2_move;
            else if (playerNumber == 2)
                img = Game.Properties.Resources.player3_move;
            else if (playerNumber == 3)
                img = Game.Properties.Resources.player4_move;
            else if (playerNumber == 4)
                img = Game.Properties.Resources.player5_move;
            else if (playerNumber == 5)
                img = Game.Properties.Resources.player6_move;
            else if (playerNumber == 6)
                img = Game.Properties.Resources.player7_move;
            else
                img = Game.Properties.Resources.player8_move;

            return img;
        }
        #endregion

        #region Map Tools
        //Prüft ob ein Wirbel an angegebener Position ist
        public static bool containsWhirl(Point pos)
        {
            foreach (BackGroundObject p in Session.bgObjects)
                if (p.position == pos)
                    if(p.GetType() == typeof(Whirl))
                        return true;
            return false;
        }

        //Prüft ob ein Spieler an angegebener Position ist
        public static bool containsPlayer(Point pos)
        {
            foreach (Player p in Session.players)
                if (p.pos == pos)
                    return true;
            return false;
        }

        //Kollision zu Spielern
        public static bool playerCollision(Point pos)
        {
            if (Tools.containsPlayer(pos))
                return true;
            else
                return false;
        }
        
        //TileID lesen und Rotation aufsplitten
        public static List<string> readTile(String tID)
        {
            string[] splitted = tID.Split(':');
            string rotation = "N";
            string tileID = "0";

            if (splitted.Length == 1)
                tileID = splitted[0];
            else if (splitted.Length == 2)
            {
                rotation = splitted[1];
                tileID = splitted[0];
            }

            return new List<string>() { tileID, rotation };
        }

        //Tile aus Map nach TileID und Rotation aufsplitten
        public static List<string> readTile(int row, int col, string[][] map)
        {
            string[] splitted = map[row][col].Split(':');
            string rotation = "N";
            string tileID = "0";

            if (splitted.Length == 1)
                tileID = splitted[0];
            else if (splitted.Length == 2)
            {
                rotation = splitted[1];
                tileID = splitted[0];
            }

            return new List<string>() { tileID, rotation };
        }

        //Kollision einer bestimmten Position erfassen
        public static bool positionCollision(Point pos)
        {
            //Falls Position außerhalb der Map dann True
            if (pos.Y < 0 || pos.X < 0 || pos.X >= Session.map[0].Length || pos.Y >= Session.map.Length)
                return true;

            //TileID der Position lesen
            string tileID = Tools.readTile(Session.map[pos.Y][pos.X])[0];

            //TileID 0 ist "nichts" also keine Kollision
            if (tileID == "0")
                return false;

            //Tile von ID holen
            Tile t = Session.tileset.getTileByID(tileID);

            //Kollisionwert des Tiles zurückgeben
            if (t != null)
                return t.collissionEnabled();

            //Falls nichts funktioniert einfach false returnen
            return false;
        }

        //Spieler an Position finden
        public static Player getPlayerbyPos(Point p)
        {
            foreach (Player pl in Session.players)
                if (pl.pos == p)
                    return pl;

            return null;
        }

        //Tile ID einer bestimmten map Position
        public static string getTile(Point pos)
        {
            if (pos.X < 0 || pos.Y < 0 || pos.Y >= Session.map.Length || pos.X >= Session.map[0].Length)
                return "0";

            return Session.map[pos.Y][pos.X];
        }

        //Tile ID einer bestimmten Hintergrund map Position
        public static string getBgTile(Point pos)
        {
            if (pos.X < 0 || pos.Y < 0 || pos.Y >= Session.map.Length || pos.X >= Session.map[0].Length)
                return "0";

            return Session.bgmap[pos.Y][pos.X];
        }
        #endregion


    }
}
